<template>
	<view class="page">
		<flowbeforeNew :beflows="beflows" :status="status" :isBtnClick="isBtnClicks" @save="save" @submit="submit" @withdraw="withdraw" :statusType='statusType'>
			<template>
				<view class="padding-bottom-100">
					<form class="flowform" @reset="reset" @submit="submit">
						<view class="formlist inputtext">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>订单编号</label></view>
								<view class="controlvalue">
									<input type="text" v-model="order.orderCode" name="orderCode" disabled="true" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="customerName">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>客户名称</label></view>
								<view class="controlvalue">
									<input type="text" v-model="order.customerName" name="customerName" placeholder="选择客户名称" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="orderDate">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>订单日期</label></view>
								<view class="controlvalue">
									<input type="text" v-model="order.orderDate" name="orderDate" placeholder="选择订单日期" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="salesmanName">
							<view class="formgroup">
								<view class="labletitle"><label>业务人员</label></view>
								<view class="controlvalue">
									<input type="text" v-model="order.salesmanName" name="salesmanName" placeholder="选择业务人员" :disabled="disabled" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext margin-top-20" @tap="transportMode">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>运输方式</label></view>
								<view class="controlvalue">
									<input type="text" v-model="order.transportMode" name="transportMode" placeholder="选择运输方式" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="paymentMode">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>付款方式</label></view>
								<view class="controlvalue">
									<input type="text" v-model="order.paymentMode" name="paymentMode" placeholder="选择付款方式" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist inputtext ">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>付款金额</label></view>
								<view class="controlvalue">
									<input type="digit" v-model="order.receivableMoney" name="receivableMoney" placeholder="输入发货金额" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext ">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>定金比率</label></view>
								<view class="controlvalue">
									<input type="digit" v-model="order.earnestRate" name="earnestRate" placeholder="输入定金比率" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext ">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>预付定金</label></view>
								<view class="controlvalue">
									<input type="digit" v-model="order.prepayEarnest" name="prepayEarnest" placeholder="输入发货金额" :disabled="disabled" />
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="deliveryDate">
							<view class="formgroup border-bottom-E3E3E5">
								<view class="labletitle"><label>发货日期</label></view>
								<view class="controlvalue">
									<input type="text" v-model="order.deliveryDate" name="deliveryDate" placeholder="选择发货日期" disabled="true" />
								</view>
								<view class="advanceicon iconfont icon-xiangyou"></view>
							</view>
						</view>
						<view class="formlist">
							<view class="formgroup  padding-17 border-bottom-E3E3E5">
								<view class="labletitletar"><label>发货地址</label></view>
								<view class="controlvalue  uni-textarea">
									<textarea v-model="order.deliveryAddress" name="deliveryAddress" maxlength="500" :disabled="disabled"
									 placeholder="输入" />
									</view>
							</view>
						</view>
						<view class="formlist">
							<view class="formgroup padding-17 border-bottom-E3E3E5">
								<view class="labletitletar"><label>订单备注</label></view>
								<view class="controlvalue  uni-textarea">
									<textarea v-model="order.description" name="description" maxlength="500" :disabled="disabled" placeholder="输入"/>
								</view>
							</view>
						</view>
						
						
						
						<!-- <view class="formlist inputtext">
							<view class="formgroup">
								<view class="labletitle"><label>相关图片</label></view>
							</view>
						</view>
						<upload-deg :upImgConfig="upImgBasic" :fileList="fileList" @onUpImg="upBasicData"></upload-deg> -->
						
						<view class="formlist inputtext" style="height: 100%; padding: 20rpx" >
							<!-- <view class="formgroup">
								<view class="labletitle"><label>相关图片</label></view>
							</view> -->
							<!-- <upload-annx :upImgConfig="upImgBasic" :fileList="fileList" @onUpImg="upBasicData" :count='4'/> -->
							
						</view>
						<JNPFUpload v-model="fileList" :fileSize='2' :limit='9'></JNPFUpload>
						
						<!-- #ifndef H5 -->
						<view class="uni-list" v-for="(item, key) in goodsList" :key="key">
						<!-- #endif -->
						<!-- #ifdef H5 -->
						<view class="uni-list" v-for="(item, key) in goodsList" :key="'detailpro'+key">
						<!-- #endif -->
							<view class="formgtitle">
								<view class="formgroup">
									<view class="labletitle font-size24"><label>商品添购（{{key+1}}）</label></view>
									<view v-if="goodsList.length>1" class="controlvalue text-align-right font-size24"><text class="color-4869EE" @tap="removeDetailpro(key)">删除</text></view>
								</view>
							</view>
							<view class="formlist inputtext" @tap="goodsName(key)">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>商品名称</label></view>
									<view class="controlvalue">
										<input type="text" v-model="item.goodsName" name="goodsName" placeholder="选择" :disabled="disabled" />
									</view>
									<view class="advanceicon iconfont icon-xiangyou"></view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>规格类型</label></view>
									<view class="controlvalue">
										<input type="text" name="specifications" v-model="item.specifications" placeholder="输入规格类型" disabled="true" />
									</view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>单位</label></view>
									<view class="controlvalue">
										<input type="text" name="unit" v-model="item.unit" placeholder="输入单位" disabled="true"/>
									</view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>数量</label></view>
									<view class="controlvalue">
										<input type="digit" name="qty" v-model="item.qty"  @blur="blurQty(key)" placeholder="输入数量" />
									</view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>单价</label></view>
									<view class="controlvalue">
										<input type="digit" name="price" v-model="item.price" @blur="blurPrice(key)" placeholder="输入单价" />
									</view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>金额</label></view>
									<view class="controlvalue">
										<input type="digit" name="amount" v-model="item.amount" placeholder="输入金额" />
									</view>
								</view>
							</view>
							<view class="formlist">
								<view class="formgroup  padding-17 border-bottom-E3E3E5">
									<view class="labletitletar"><label>备注</label></view>
									<view class="controlvalue  uni-textarea">
										<textarea v-model="item.description" name="description" maxlength="500" :disabled="disabled" placeholder="备注" />
										</view>
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="addDetailpro" v-if="status != '1' && status != '2'">
							<view class="formgroup">
								<view class="second-title color-4869EE"><label class="iconfont icon-jia font-size28"></label>商品添购</view>
							</view>
						</view>
						
						
						
						<!-- #ifndef H5 -->
						<view class="uni-list" v-for="(collection, key) in collectionPlanList" :key="key">
						<!-- #endif -->
						
						<!-- #ifdef H5 -->
						<view class="uni-list" v-for="(collection, key) in collectionPlanList" :key="'collection'+key">
						<!-- #endif -->
							<view class="formgtitle">
								<view class="formgroup">
									<view class="labletitle font-size24"><label>收款计划({{key+1}})</label></view>
									<view v-if="collectionPlanList.length>1" class="controlvalue text-align-right font-size24"><text class="color-4869EE" @tap="removeCollection(key)">删除</text></view>
								</view>
							</view>
							<view class="formlist inputtext" @tap="receivableDate(key)">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>收款日期</label></view>
									<view class="controlvalue">
										<input type="text" v-model="collection.receivableDate" name="receivableDate" placeholder="选择收款日期" disabled="true" />
									</view>
									<view class="advanceicon iconfont icon-xiangyou"></view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>收款比率%</label></view>
									<view class="controlvalue">
										<input type="digit" v-model="collection.receivableRate" name="receivableRate" placeholder="输入收款比率" />
									</view>
								</view>
							</view>
							<view class="formlist inputtext">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>收款金额</label></view>
									<view class="controlvalue">
										<input type="digit" v-model="collection.receivableMoney" name="receivableMoney" placeholder="输入收款金额" />
									</view>
								</view>
							</view>
							<view class="formlist inputtext" @tap="receivableMode(key)">
								<view class="formgroup border-bottom-E3E3E5">
									<view class="labletitle"><label>收款方式</label></view>
									<view class="controlvalue">
										<input type="text" v-model="collection.receivableMode" name="receivableMode" placeholder="选择收款方式" disabled="true" />
									</view>
									<view class="advanceicon iconfont icon-xiangyou"></view>
								</view>
							</view>
							<view class="formlist">
								<view class="formgroup  padding-17 border-bottom-E3E3E5">
									<view class="labletitletar"><label>收款摘要</label></view>
									<view class="controlvalue  uni-textarea">
										<textarea v-model="collection.description" name="description" maxlength="500" :disabled="disabled"
										 placeholder="输入摘要" />
										</view>
								</view>
							</view>
						</view>
						<view class="formlist inputtext" @tap="addCollection" v-if="status != '1' && status != '2'">
							<view class="formgroup">
								<view class="second-title color-4869EE"><label class="iconfont icon-jia font-size28"></label>收款计划</view>
							</view>
						</view>
					</form>
					<view class="height-100"></view>
					<w-picker :mode="mode" :pickerList="pickerList" ref="picker" :isSearchShow="isSearchShow"></w-picker>
					<tki-tree ref="tree" :range="treelist" :multiple="multiple" :selectParent="selectParent" rangeKey="fullName"></tki-tree>
				</view>
			</template>
		</flowbeforeNew>
		<w-picker :mode="mode" :pickerList="pickerList" ref="picker" :isSearchShow="isSearchShow"></w-picker>
	</view>
</template>

<script>
	import config from '@/common/js/config.js'
	import helper from '@/common/js/helper.js'
	import wPicker from '@/components/w-picker/w-picker.vue'
	import tkiTree from '@/components/tki-tree/tki-tree.vue'
	import graceChecker from '@/common/js/graceChecker.js'
	import flowbeforeNew from '@/components/work/flow-beforeNew.vue'
	let formRule=[
			{name:"customerName",checkType:"notnull",checkRule:"",errorMsg:"选择客户名称"},
			{name:"salesmanName", checkType : "notnull", checkRule:"", errorMsg:"选择业务人员"},
			{name:"paymentMode", checkType : "notnull", checkRule:"", errorMsg:"选择付款方式"},
			{name:"deliveryDate",checkType:"notnull",checkRule:"",errorMsg:"选择发货日期"},
		];
	let collectionRule=[
		{name:"receivableDate",checkType:"notnull",checkRule:"",errorMsg:"选择收款日期"},
		{name:"receivableMoney", checkType : "money", checkRule:"", errorMsg:"输入收款金额"},
		{name:"receivableRate", checkType : "money", checkRule:"",  errorMsg:"输入收款比率"},
		]
	let detailproRule=[
		{name:"goodsName",checkType:"notnull",checkRule:"",errorMsg:"选择商品名称"},
		]
	export default {
		components: {
			tkiTree,
			wPicker,
			flowbeforeNew
		},
		data() {
			return {
				statusType:null,
				beflows:{
					operstate:"",
					processId:''
				},
				isBtnClicks:false,
				status: null,
				order: {
					orderCode:'',
					customerName:'',
					deliveryDate:'',
					salesmanName:'',
					paymentMode:'',
					transportMode:'',
					fileJson:'[]'
				},
				
				detailproList:[],
				disabled:false,
				fileList:[],
				upImgBasic: {
					notli: true,
					isShowAdd:true,
					isEdit:false
				},
				fileJson:[],
				pickerList:[],
				isSearchShow:false,
				mode:'',//底部弹窗分类
				salesmanNameList:[],
				treelist:[],
				customerNameList:[],
				paymentModeList: [{
						label: "现金",
						value: "1"
					},
					{
						label: "转帐",
						value: "2"
					},
					{
						label: "汇票",
						value: "3"
					}
				],
				transportModeList: [{
						label: "快递",
						value: "1"
					},
					{
						label: "物流",
						value: "2"
					},
					{
						label: "配送",
						value: "3"
					},
					{
						label: "自提",
						value: "4"
					}
				],
				multiple:false,
				selectParent:false,
				goodsNameList:[],
				collectionPlanList:[],
				goodsList:[]
			}
		},
		
		onBackPress(e){
			/* 删除缓存 */
			uni.removeStorageSync("listTitle")
			uni.removeStorageSync("startedTitle")
			uni.removeStorageSync("F_FullName")
		},
		
		onLoad(e) {
			let statusType = uni.getStorageSync("type");
			
			if(statusType){
				this.statusType = statusType
			}else{
				this.statusType = 'started'
			}
			
			
			/* 动态修改标题 */
			let title = uni.getStorageSync("listTitle");
			let title2 = uni.getStorageSync("startedTitle");
			let title3 = uni.getStorageSync("F_FullName");
			if(title){
				this.title = title
			}else if(title2){
				this.title = title2
			} else if(title3){
				this.title = title3
			}
			
			/* 设置顶部标题 */
			uni.setNavigationBarTitle({
				title: this.title
			});
			const userProvider = uni.getStorageSync(config.Global.cache + '_UserProvider');
			this.beflows.operstate = e.status;
			this.beflows.processId = e.id
			
			if (Object.prototype.hasOwnProperty.call(e, 'id')) {
				/* 保存状态码 */
				this.status = this.beflows.operstate
				uni.setStorageSync('F_Status',Number(this.status))
				if(this.status == 1 || this.status == 2 || this.status == 'submit'){
					this.disabled = true
				}
				
				
				helper.ajax({
					url: '/api/WorkFlowForm/CrmOrder/' + this.beflows.processId,
					success: res => {
						let _order = res.data.data;
						this.order = _order;
						console.log(this.order)
						this.collectionPlanList = this.order.collectionPlanList
						for(let i = 0;i<this.collectionPlanList.length;i++){
							this.collectionPlanList[i].receivableDate = helper.toDate( this.collectionPlanList[i].receivableDate, 'yyyy-MM-dd')
						}
						this.goodsList = this.order.goodsList
						this.order.orderDate=helper.toDate(this.order.orderDate, 'yyyy-MM-dd');
						this.order.deliveryDate=helper.toDate(this.order.deliveryDate, 'yyyy-MM-dd');
						// this.detailproList= _order.goodsList;
						// this.collectionPlanList = _order.collectionPlanList;
						// for (var i = 0; i < _order.collectionPlanList.length; i++) {
						// 	this.collectionPlanList[i].receivableDate=helper.toDate( _order.collectionPlanList[i].receivableDate, 'yyyy-MM-dd');
						// }
						if (_order.fileJson != "") {
							this.fileList=JSON.parse(_order.fileJson);
						}
						// this.upImgBasic.isEdit=true;
						// if(!this.disabled){
						// 	this.upImgBasic.isShowAdd=true;
						// }else{
						// 	this.upImgBasic.isShowAdd=false;
						// }
					}
				});
			}else{
				
				this.status = 1;
				helper.ajax({
					url: '/api/System/BillRule/BillNumber/OrderNumber',
					success: res => {
						this.order.orderCode=res.data.data;
					}
				});
				this.collectionPlanList.push({
					receivableDate:'',
					receivableMode:'',
					receivableRate:'',
					receivableMoney:'',
					description:''
				});
				this.detailproList.push({
					goodsName:"",
					specifications:'',
					unit:'',
					qty:'',
					price:'',
					amount:'',
					description:''
				});
			}
			helper.ajax({
				url: '/api/Permission/Users/Selector',
				loadingShow:false,
				success: res => {
					this.salesmanNameList = res.data.data.list;
				}
			});
			helper.ajax({
				url: '/api/WorkFlowForm/CrmOrder/Customer',
				loadingShow:false,
				data:{keyword:''},
				success: res => {
					let customers= res.data.data.list;
					for (var i = 0; i < customers.length; i++) {
						this.customerNameList.push({
							label: customers[i].text,
							value: customers[i].id,
							code: customers[i].code,
							keyword: customers[i].keyword,
						});
					}
				}
			});
			helper.ajax({
				url: '/api/WorkFlowForm/CrmOrder/Goods',
				loadingShow:false,
				data:{keyword:''},
				success: res => {
					let goods = res.data.data.list;
					for (var i = 0; i < goods.length; i++) {
						this.goodsNameList.push({
							label: goods[i].text,
							value: goods[i].code,
							id: goods[i].id,
							keyword: goods[i].keyword,
							price:goods[i].price,
							unit:goods[i].unit,
							specifications:goods[i].specifications,
						});
					}
				}
			});
		},
		
		
		onShow(e) {
			/* 动态修改标题 */
			uni.setNavigationBarTitle({
				title:this.title
			});
		},
		
		
		
		methods: {
			salesmanName() {
				this.isSearchShow=false;
				this.treelist = this.salesmanNameList;
				this.selectParent = false;
				this.multiple = false;
				if(!this.disabled){
					this.$refs.tree._show();
				}
				this.$eventHub.$off('treemodality');
				this.$eventHub.$on('treemodality', (data) => {
					this.order.salesmanName = data[0].fullName;
					this.order.salesmanId = data[0].id;
					this.$eventHub.$off('treemodality');
				});
			},
			customerName(){
				if(!this.disabled){
					this.isSearchShow=false;
					this.isSearchShow=true;
					this.pickerList = this.customerNameList;
					this.mode="electionsingle";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.order.customerName = data.result;
						this.$eventHub.$off('popupmodality');
						this.isSearchShow=false;
					});
				}
			},
			paymentMode(){
				if(!this.disabled){
					this.isSearchShow=false;
					this.pickerList = this.paymentModeList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.order.paymentMode = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			transportMode(){
				if(!this.disabled){
					this.isSearchShow=false;
					this.pickerList = this.transportModeList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.order.transportMode = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			orderDate(){
				if(!this.disabled){
					this.mode="dateTime";
					this.isSearchShow=false;
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.order.orderDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			deliveryDate(){
				if(!this.disabled){
					this.mode="date";
					this.isSearchShow=false;
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.order.deliveryDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			receivableDate(index){
				if(!this.disabled){
					this.mode="date";
					this.isSearchShow=false;
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.collectionPlanList[index].receivableDate = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			receivableMode(index){
				if(!this.disabled){
					this.isSearchShow=false;
					this.pickerList = this.paymentModeList;
					this.mode="selector";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.collectionPlanList[index].receivableMode = data.result;
						this.$eventHub.$off('popupmodality');
					});
				}
			},
			goodsName(index){
				let _this = this;
				if(!this.disabled){
					this.isSearchShow=true;
					this.pickerList = this.goodsNameList;
					this.mode="electionsingle";
					this.$refs.picker.show();
					this.$eventHub.$off('popupmodality');
					this.$eventHub.$on('popupmodality', (data) => {
						this.detailproList[index].goodsName = data.result;
						this.detailproList[index].specifications=this.goodsNameList[data.defaultVal[0]].specifications;
						this.detailproList[index].unit=this.goodsNameList[data.defaultVal[0]].unit;
						this.detailproList[index].qty=1;
						this.detailproList[index].price=this.goodsNameList[data.defaultVal[0]].price;
						this.detailproList[index].amount=(this.goodsNameList[data.defaultVal[0]].price*this.detailproList[index].qty).toFixed(2);
						this.$eventHub.$off('popupmodality');
						this.isSearchShow=false;
					});
				}
			},
			blurQty(index){
				this.detailproList[index].amount=(this.detailproList[index].price*this.detailproList[index].qty).toFixed(2);
			},
			blurPrice(index){
				this.detailproList[index].amount=(this.detailproList[index].qty*this.detailproList[index].price).toFixed(2);
			},
			addDetailpro(){
				this.detailproList.push({
					goodsName:"",
					specifications:'',
					unit:'',
					qty:'',
					price:'',
					amount:'',
					description:''
				});
			},
			addCollection(){
				this.collectionPlanList.push({
					receivableDate:'',
					receivableMode:'',
					receivableRate:'',
					receivableMoney:'',
					description:''
				});
			},
			removeDetailpro(index){
				this.detailproList.splice(index,1);
			},
			removeCollection(index){
				this.collectionPlanList.splice(index,1);
			},
			async upBasicData(e) {
				// console.log(e)
				let arrImg = [];
				let fileJson=[];
				for (let i = 0, len = e.length; i < len; i++) {
					if (e[i].fileState == "success") {
						fileJson.push({
							fileId: e[i].fileId,
							fileName: e[i].fileName,
							fileSize: e[i].fileSize,
							fileState: e[i].fileState,
							fileTime: e[i].fileTime,
							fileType: 3
						});
					}
					if (e[i].path != "") {
						await arrImg.push(e[i].path.split(','));
					}
				}
				this.fileJson = fileJson;
				this.order.fileJson=JSON.stringify(fileJson);
			},
			
			/* 保存 */
			save(e){
				this.order.fileJson = JSON.stringify(this.fileList)
				let formData = this.order
				// console.log(formData)
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return;
				}
				for(let i=0;i<this.detailproList.length;i++ ){
					if (!graceChecker.check(this.detailproList[i], detailproRule)) {
						helper.msg(graceChecker.error);
						return;
					}
				}
				for(let i=0;i<this.collectionPlanList.length;i++ ){
					if (!graceChecker.check(this.collectionPlanList[i], collectionRule)) {
						helper.msg(graceChecker.error);
						return;
					}
				}
				let url = "/api/WorkFlowForm/CrmOrder";
				let method = "POST";
				if(formData.id){
					url = url+"/"+formData.id;
					method = "PUT";
				}
				formData.customerId=helper.setSpinner(this.customerNameList,formData.customerName);
				formData.deliveryDate = Date.parse(formData.deliveryDate);
				formData.orderDate = Date.parse(formData.orderDate);
				for (var i = 0; i < this.collectionPlanList.length; i++) {
					this.collectionPlanList[i].receivableDate=Date.parse(this.collectionPlanList[i].receivableDate);
				}
			formData.goodsList = this.detailproList;
			formData.collectionPlanList = this.collectionPlanList;
			formData.status = 1;
				helper.ajax({
					url: url,
					method: method,
					contentType:"application/json",
					data:formData,
					success: res => {
						helper.msg(res.data.msg,'success');
						uni.navigateBack();
					}
				});
			},
			
			/* 提交 */
			submit(e){
				this.order.fileJson = JSON.stringify(this.fileList)
				let formData=this.order;
				
				if (!graceChecker.check(formData,formRule)) {
					helper.msg(graceChecker.error);
					return
				}
				let _this = this;
				this.isBtnClicks=true;
				this.treelist = this.salesmanNameList;
				this.$refs.tree._show();
				this.$eventHub.$off('treemodality');
				this.$eventHub.$on('treemodality', (data) => {
					this.$eventHub.$off('treemodality');
					formData.customerId=helper.setSpinner(_this.customerNameList,formData.customerName);
					formData.goodsList = this.detailproList;
					formData.deliveryDate = Date.parse(formData.deliveryDate);
					formData.orderDate = Date.parse(formData.orderDate);
					formData.status = 0;
					formData.freeApproverUserId = data[0].id;
					let method = "POST";
					let url = "/api/WorkFlowForm/CrmOrder";
					
					if(formData.id){
						method = "PUT";
						url = url+"/"+formData.id
					}
					for (var i = 0; i < this.collectionPlanList.length; i++) {
						this.collectionPlanList[i].receivableDate=Date.parse(this.collectionPlanList[i].receivableDate);
					}
					formData.collectionPlanList = this.collectionPlanList;
					helper.ajax({
						url: url,
						method:method,
						data:formData,
						contentType: "application/json",
						success: res => {
						helper.msg(res.data.msg, 'success');
						
								// uni.redirectTo({
								// 	url: '../../operate/started/started',
								// 	success: res => {},
								// 	fail: () => {},
								// 	complete: () => {}
								// });
							
								uni.navigateBack();
							
							_this.isBtnClicks=false
						}
					});
				});
			},
			
			
			/* 清空数据 */
			reset(e) {
				// console.log('清空数据')
			},
			
			/* 撤回 */
			withdraw(e){
				// console.log(this.status)
				if (this.status == '1') { //审核撤回
					this.bus.$off('auditopinion');
					this.bus.$on('auditopinion', (data) => {
						uni.showLoading({
							title: '正在请求...',
							mask: true
						});
						this.isBtnClicks = true;
						let formData=this.order;
						// console.log(data)
						helper.ajax({
							url: `/api/WorkFlow/FlowLaunch/${formData.id}/Actions/Withdraw`,
							method: 'PUT',
							contentType: "application/json",
							data: {
								handleOpinion: data.F_HandleOpinion,
							},
							success: res => {
								helper.msg(res.data.meg, 'success');
								// console.log(res.data)
								// uni.redirectTo({
								// 	url: '/pages/apply/apply/ordermanagelist/index'
								// });
								
								uni.navigateBack();
								
								setTimeout(function() {
									uni.hideLoading();
									this.isBtnClicks = false
								}, 500);
							},
						});
						this.bus.$off('auditopinion');
					});
					uni.navigateTo({
						url:'../../../work/operate/auditopinion/auditopinion?opinion=撤回',
						success: res => {},
						fail: () => {},
						complete: () => {}
					});
				}
			}
		},
	}
</script>

<style>
	page {
		background-color: #f0f2f6;
	}
	.btn-submit{
		width: 50%;
	}
</style>
